<template>
  <div class="dialog-overlay" v-if="visible">
    <div class="dialog-container">
      <div class="dialog-header">
        <h3>新增员工</h3>
        <button class="close-btn" @click="handleClose">×</button>
      </div>

      <div class="dialog-body">
        <form class="employee-form">
          <div class="form-group">
            <label>姓名 <span class="required">*</span></label>
            <input 
              type="text" 
              v-model="employee.empName" 
              placeholder="请输入姓名"
            >
          </div>

          <div class="form-group">
            <label>性别</label>
            <select v-model="employee.gender">
              <option value="">请选择</option>
              <option value="男">男</option>
              <option value="女">女</option>
            </select>
          </div>

          <div class="form-group">
            <label>部门ID <span class="required">*</span></label>
            <input 
              type="number" 
              v-model="employee.deptId" 
              placeholder="请输入部门ID"
            >
          </div>

          <div class="form-group">
            <label>职位</label>
            <input 
              type="text" 
              v-model="employee.position" 
              placeholder="请输入职位"
            >
          </div>

          <div class="form-group">
            <label>电话 <span class="required">*</span></label>
            <input 
              type="text" 
              v-model="employee.phone" 
              placeholder="请输入电话号码"
            >
          </div>

          <div class="form-group">
            <label>邮箱</label>
            <input 
              type="email" 
              v-model="employee.email" 
              placeholder="请输入邮箱"
            >
          </div>

          <div class="form-group">
            <label>入职日期</label>
            <input 
              type="date" 
              v-model="employee.hireDate"
            >
          </div>

          <div class="form-group">
            <label>地址</label>
            <textarea 
              v-model="employee.address" 
              placeholder="请输入地址"
              rows="3"
            ></textarea>
          </div>
        </form>
      </div>

      <div class="dialog-footer">
        <button class="cancel-btn" @click="handleClose">取消</button>
        <button class="confirm-btn" @click="handleSubmit">确定新增</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AddEmployeeDialog',
  props: {
    // 控制弹窗显示/隐藏
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 员工信息对象，与接口参数对应
      employee: {
        empId: null, // 新增时通常由后端生成，可不填
        loginId: null, // 若与用户系统关联，可能需要填写
        empName: '',
        gender: '',
        deptId: null,
        position: '',
        phone: '',
        email: '',
        hireDate: '',
        resignDate: null, // 新增时默认为在职，可不填
        address: '',
        updateTime: null // 通常由后端生成
      }
    }
  },
  methods: {
    // 关闭弹窗
    handleClose() {
      this.$emit('close')
      this.resetForm()
    },

    // 提交新增
    handleSubmit() {
      // 简单表单验证
      if (!this.employee.empName) {
        alert('请输入姓名')
        return
      }
      if (!this.employee.deptId) {
        alert('请输入部门ID')
        return
      }
      if (!this.employee.phone) {
        alert('请输入电话号码')
        return
      }

      // 格式化日期（若有）
      if (this.employee.hireDate) {
        this.employee.hireDate = new Date(this.employee.hireDate).toISOString()
      }

      // 触发父组件的新增方法，并传递员工数据
      this.$emit('submit', { ...this.employee })
    },

    // 重置表单
    resetForm() {
      this.employee = {
        empId: null,
        loginId: null,
        empName: '',
        gender: '',
        deptId: null,
        position: '',
        phone: '',
        email: '',
        hireDate: '',
        resignDate: null,
        address: '',
        updateTime: null
      }
    }
  }
}
</script>

<style scoped>
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.dialog-container {
  width: 500px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.dialog-header {
  padding: 16px 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-header h3 {
  margin: 0;
  font-size: 18px;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #999;
}

.close-btn:hover {
  color: #333;
}

.dialog-body {
  padding: 20px;
}

.employee-form {
  display: grid;
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  font-size: 14px;
  color: #333;
}

.required {
  color: #ff4d4f;
}

.form-group input,
.form-group select,
.form-group textarea {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.form-group textarea {
  resize: vertical;
}

.dialog-footer {
  padding: 16px 20px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.cancel-btn, .confirm-btn {
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.cancel-btn {
  border: 1px solid #ddd;
  background-color: white;
  color: #333;
}

.confirm-btn {
  border: none;
  background-color: #1890ff;
  color: white;
}

.confirm-btn:hover {
  background-color: #096dd9;
}
</style>